<?php
class ModelingController extends Zend_Controller_Action
{
	public function init()
	{
		//$this->_helper->layout()->setLayout('tmplayout');
		$this->_helper->layout()->disableLayout();
	}
	public function indexAction()
	{
		
	}
	public function addAction()
	{		
		if ($this->getRequest()->isPost()) 
        {
            $formData = $this->getRequest()->getPost();
            $name = $formData['name'];
            $displayname = $formData['displayname'];
            
            $svc = new App_Service_Doctrine();
            $ret = $svc->createEntityXml($name, $displayname);
            
            return $this->_helper->json(array('success' => $ret != false, 'name'=>$svc->getModelNamePrefix() . $name));
        }
	}
	public function editAction()
	{		
		$this->view->entity = $this->getRequest()->getParam('name');
		
		$svc = new App_Service_Doctrine();
		$this->view->models = $svc->getAllEntites();
	}
	
	public function deleteAction()
	{
		$result = 'false';
        if ($this->getRequest()->isPost()) {
            $name = $this->getRequest()->getParam('name');
            $svc = new App_Service_Doctrine();
            $result = $svc->deleteEntity($name);            
        }
        return $this->_helper->json(array('success' => $result));
	}
	
	public function modelsAction()
	{
		$svc = new App_Service_Doctrine();
		$models = $svc->getAllEntites();
		return $this->_helper->json(array('totalCount'=>count($models), 'source'=>$models));
	}
	
	public function fieldsAction()
	{
		$svc = new App_Service_Doctrine();
		$name = $this->getRequest()->getParam('name');
		return $this->_helper->json($svc->getEntityFields($name));
	}
	
	public function many2oneAction()
	{
		$svc = new App_Service_Doctrine();
		$name = $this->getRequest()->getParam('name');
		return $this->_helper->json($svc->getMany2One($name));
	}
	
	public function many2manyAction()
	{
		$svc = new App_Service_Doctrine();
		$name = $this->getRequest()->getParam('name');
		return $this->_helper->json($svc->getMany2Many($name));	
	}
	
	public function updatefieldAction()
	{
		if ($this->getRequest()->isPost()) {
            $entity = $this->getRequest()->getParam('entity');
            $svc = new App_Service_Doctrine();
            $data = $this->getRequest()->getPost();
            $result = $svc->updateEntityField($entity, $data);
            return $this->_helper->json(array('success' => $result));
		 }
	}
	public function updatemany2oneAction()
	{
		if ($this->getRequest()->isPost()) {
            $entity = $this->getRequest()->getParam('entity');
            $svc = new App_Service_Doctrine();
            $data = $this->getRequest()->getPost();
            $result = $svc->updateMany2One($entity, $data);
            return $this->_helper->json(array('success' => $result));
		 }
	}
	public function updatemany2manyAction()
	{
		if ($this->getRequest()->isPost()) {
            $entity = $this->getRequest()->getParam('entity');
            $svc = new App_Service_Doctrine();
            $data = $this->getRequest()->getPost();
            $result = $svc->updateMany2Many($entity, $data);
            return $this->_helper->json(array('success' => $result));
		 }
	}
	public function removefieldAction()
	{
		if ($this->getRequest()->isPost()) {
            $entity = $this->getRequest()->getParam('entity');
            $svc = new App_Service_Doctrine();
            $data = $this->getRequest()->getPost();
            $result = $svc->removeEntityField($entity, $data);
            return $this->_helper->json(array('success' => $result));
		 }
	}
	
	public function removemany2oneAction()
	{
		if ($this->getRequest()->isPost()) {
            $entity = $this->getRequest()->getParam('entity');
            $svc = new App_Service_Doctrine();
            $data = $this->getRequest()->getPost();
            $result = $svc->removeMany2One($entity, $data);
            return $this->_helper->json(array('success' => $result));
		 }
	}
	
	public function removemany2manyAction()
	{
		if ($this->getRequest()->isPost()) {
            $entity = $this->getRequest()->getParam('entity');
            $svc = new App_Service_Doctrine();
            $data = $this->getRequest()->getPost();
            $result = $svc->removeMany2Many($entity, $data);
            return $this->_helper->json(array('success' => $result));
		 }
	}
	
	public function runAction()
	{
		if ($this->getRequest()->isPost()) {
			$svc = new App_Service_Doctrine();
			$result = $svc->runModeling();
            return $this->_helper->json(array('success' => $result));	
		}
	}
	
}